package com.sxg.controller;

import com.sxg.convert.SxgImagesConvert;
import com.sxg.entity.SxgImagesEntity;
import com.sxg.query.SxgImagesQuery;
import com.sxg.vo.SxgImagesVO;
import lombok.AllArgsConstructor;
import com.sxg.page.PageResult;
import com.sxg.utils.code.Result;
import com.sxg.service.SxgImagesService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
* 商品图片
*
* @author 浪漫写尽歌谱 3298751420@qq.com
* @since 1.0.0 2024-03-07
*/
@RestController
@RequestMapping("sxg/sxg_images")
@AllArgsConstructor
public class SxgImagesController {
    private final SxgImagesService sxgImagesService;

    @GetMapping("page")
    @PreAuthorize("hasAuthority('sxg:sxg_images:page')")
    public Result<PageResult<SxgImagesVO>> page(@Valid SxgImagesQuery query){
        PageResult<SxgImagesVO> page = sxgImagesService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @PreAuthorize("hasAuthority('sxg:sxg_images:info')")
    public Result<SxgImagesVO> get(@PathVariable("id") Long id){
        SxgImagesEntity entity = sxgImagesService.getById(id);

        return Result.ok(SxgImagesConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @PreAuthorize("hasAuthority('sxg:sxg_images:save')")
    public Result<String> save(@RequestBody SxgImagesVO vo){
        sxgImagesService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @PreAuthorize("hasAuthority('sxg:sxg_images:update')")
    public Result<String> update(@RequestBody @Valid SxgImagesVO vo){
        sxgImagesService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @PreAuthorize("hasAuthority('sxg:sxg_images:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        sxgImagesService.delete(idList);

        return Result.ok();
    }

}